package com.ftg.learn.day10.reflech;

import com.ftg.learn.day08.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author 布衣
 */
public class TestStuEntity {

    public static void main(String[] args) {
        System.out.println("第一种方式");
        String path = "com.ftg.learn.day10.reflech.StuEntity";
        try {
            Class<StuEntity> obj = (Class<StuEntity>)Class.forName(path);

            Constructor constructor = obj.getConstructor(int.class);
            //setAccessible 可见
            constructor.setAccessible(true);
            Object obj1 = constructor.newInstance(2);

//            Constructor[] constructors = obj.getDeclaredConstructors();
//            System.out.println(constructors.length);
//            for (int i = 0; i < constructors.length; i++) {
//                System.out.println(constructors[i].getName());
//            }


            StuEntity stuEntity = (StuEntity)obj.newInstance();

            stuEntity.say();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        System.out.println("第二种");
        Class<StuEntity> stuEntityClass = StuEntity.class;
        try {
            StuEntity stuEntity1 = stuEntityClass.newInstance();
            stuEntity1.say();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

//        Class : 反射类
//        class Class

        System.out.println("第三种");
        StuEntity se = new StuEntity();
        try {
            StuEntity stuEntity = se.getClass().newInstance();
            stuEntity.say();
            System.out.println(se == stuEntity);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

}
